package com.hs.hlw.credit.system.web.controller.api;

import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.dto.UserDto;
import com.hs.hlw.credit.system.business.service.SubmissionFormAttachmentService;
import com.hs.hlw.credit.system.common.enumeration.ExcelTypeEnum;
import com.hs.hlw.credit.system.web.util.RequestUtil;
import com.hualongdata.exception.BusinessException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

/**
 * SubmissionFormAttachmentController
 * @author zhb
 * @date 2018/11/28
 */
@RestController
@RequestMapping("/api/submission/form/attachment")
public class SubmissionFormAttachmentController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SubmissionFormAttachmentController.class);
    private @Autowired SubmissionFormAttachmentService submissionFormAttachmentService;

    @RequestMapping(value = "downloadAttachment", method = RequestMethod.GET)
    @ApiOperation(value = "下载附件")
    public void downloadAttachment(@RequestParam @ApiParam(value = "附件id", required = true) Integer id,
                                   HttpServletResponse response) {
        submissionFormAttachmentService.downloadAttachment(id, response);
    }

    @PostMapping(path = "moveUp")
    @ApiOperation(value = "排序上移", notes = "排序上移")
    public ResponseDto moveUp(@RequestParam @ApiParam(value = "附件id", required = true) Integer id) {
        return submissionFormAttachmentService.moveUp(id);
    }

    @PostMapping(path = "moveDown")
    @ApiOperation(value = "排序下移", notes = "排序下移")
    public ResponseDto moveDown(@RequestParam @ApiParam(value = "附件id", required = true) Integer id) {
        return submissionFormAttachmentService.moveDown(id);
    }

    @PostMapping(path = "delete")
    @ApiOperation(value = "删除附件", notes = "删除附件")
    public ResponseDto delete(@RequestParam @ApiParam(value = "附件id", required = true) Integer id) {
        return submissionFormAttachmentService.delete(id);
    }

    @GetMapping(path = "list")
    @ApiOperation(value = "附件列表", notes = "附件列表")
    public ResponseDto list(@RequestParam @ApiParam(value = "表单id", required = true) Integer formId) {
        return submissionFormAttachmentService.list(formId);
    }

    @PostMapping(path = "updateBatchSubmissionExplain")
    @ApiOperation(value = "更新批量填写说明", notes = "更新批量填写说明")
    public ResponseDto updateBatchSubmissionExplain(@RequestParam @ApiParam(value = "表单id", required = true) Integer formId,
                                                    @RequestParam @ApiParam(value = "批量填写说明", required = true) String batchSubmissionExplain) {
        return submissionFormAttachmentService.updateBatchSubmissionExplain(formId, batchSubmissionExplain);
    }

    @RequestMapping(value = "uploadAttachment", method = RequestMethod.POST)
    @ApiOperation(value = "上传附件", notes = "上传附件")
    public ResponseDto uploadAttachment(@RequestParam @ApiParam(value = "表单id", required = true) Integer formId,
                                        @RequestParam("file") @ApiParam(value = "数据文件", required = true) MultipartFile file,
                                        HttpServletRequest request) {
        try {
            // 获取文件名和文件流
            String fileName = file.getOriginalFilename();
            FileInputStream fis = (FileInputStream) file.getInputStream();
            return submissionFormAttachmentService.uploadAttachment(formId, fileName, fis);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                BusinessException be = (BusinessException) e;
                ResponseDto responseDto = new ResponseDto(1, be.getErrMsg());
                return responseDto;
            } else {
                ResponseDto responseDto = new ResponseDto(1, "文件上传失败");
                LOGGER.error("文件上传失败", e);
                return responseDto;
            }
        }
    }
}